package team.knowers.qzh.qzhserver.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import team.knowers.qzh.qzhserver.dao.entity.CourseDO;
import team.knowers.qzh.qzhserver.dao.entity.UserDO;
import team.knowers.qzh.qzhserver.dto.req.SearchReqDTO;
import team.knowers.qzh.qzhserver.dto.resp.CourseSearchRespDTO;
import team.knowers.qzh.qzhserver.dto.resp.UserSearchRespDTO;
import team.knowers.qzh.qzhserver.service.CourseService;
import team.knowers.qzh.qzhserver.service.SearchService;
import team.knowers.qzh.qzhserver.service.UserService;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SearchServiceImpl implements SearchService {
    private final UserService userService;
    private final CourseService courseService;

    @Override
    public List<UserSearchRespDTO> listUserInfo(SearchReqDTO selectReqDTO) {
        LambdaQueryWrapper<UserDO> queryWrapper = Wrappers.lambdaQuery(UserDO.class)
                .like(UserDO::getId, selectReqDTO.getKeyword())
                .or()
                .like(UserDO::getNickname, selectReqDTO.getKeyword())
                .or()
                .like(UserDO::getUsername, selectReqDTO.getKeyword());
        List<UserDO> userDOS = userService.getBaseMapper().selectList(queryWrapper);
        List<UserSearchRespDTO> results = new ArrayList<>();
        for (UserDO suserDO : userDOS) {
            results.add(BeanUtil.toBean(suserDO, UserSearchRespDTO.class));
        }
        return results;
    }

    @Override
    public List<CourseSearchRespDTO> listCourseInfo(SearchReqDTO selectReqDTO) {
        LambdaQueryWrapper<CourseDO> queryWrapper = Wrappers.lambdaQuery(CourseDO.class)
                .like(CourseDO::getId, selectReqDTO.getKeyword())
                .or()
                .like(CourseDO::getName, selectReqDTO.getKeyword())
                .or()
                .like(CourseDO::getUserId, selectReqDTO.getKeyword())
                .or()
                .like(CourseDO::getSubjectId, selectReqDTO.getKeyword())
                .or()
                .like(CourseDO::getDescription, selectReqDTO.getKeyword());
        List<CourseDO> courseDOS = courseService.getBaseMapper().selectList(queryWrapper);
        List<CourseSearchRespDTO> results = new ArrayList<>();
        for (CourseDO courseDO : courseDOS) {
            results.add(BeanUtil.toBean(courseDO, CourseSearchRespDTO.class));
        }
        return results;
    }
}
